package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_RechargeCoin;
import com.kzd.sdk.ui.adapter.holder.RechargeCoinHolder;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_RechargeCoinAdapter extends RecyclerView.Adapter<RechargeCoinHolder> {

    private List<KZD_RechargeCoin.CoinInfo> data = new ArrayList<>();

    private final Context mContext;

    private int mSelectPosition = -1;

    public void setSelectPosition(int position) {
        this.mSelectPosition = position;
        this.notifyDataSetChanged();
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    private KZD_OnItemClickListener mOnItemClickListener;

    public KZD_RechargeCoinAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public RechargeCoinHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RechargeCoinHolder(new TextView(mContext));
    }

    @Override
    public void onBindViewHolder(@NonNull RechargeCoinHolder holder, int position) {
        KZD_RechargeCoin.CoinInfo itemData = get(position);
        holder.tvRechargeCoinMoney.setText(new StringBuilder(itemData.getMoney()).append("CNY"));

        if (mSelectPosition == position) {
            holder.tvRechargeCoinMoney.setTextColor(Color.parseColor("#ffffff"));
            holder.tvRechargeCoinMoney.setBackgroundResource(AppUtils.getDrawableId(mContext,"kzd_back_05dp_blue"));
        } else {
            holder.tvRechargeCoinMoney.setTextColor(Color.parseColor("#666666"));
            holder.tvRechargeCoinMoney.setBackgroundResource(AppUtils.getDrawableId(mContext,"kzd_line_05dp_gray"));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectPosition(position);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_RechargeCoin.CoinInfo> coinInfoList) {
        if (coinInfoList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = coinInfoList;
        }
        notifyDataSetChanged();
    }

    public KZD_RechargeCoin.CoinInfo get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
